﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public class LarMessageBox : GuideLikeComponent
    {
        private string title;
        public string Title
        {
            get { return title; }
            set 
            {
                TitleSize = Engine.FontManager.GetFont("Kootenay").MeasureString(value);
                title = value; 
            }
        }

        private Vector2 TitleSize;

        public event EventHandler AcceptedEvent;
        public event EventHandler DeniedEvent;
        public event EventHandler Finished;

        private List<string> Text;

        public bool ShowDenyButton = false;
        public bool DisableAfterFinish = true;

        public LarMessageBox(string Title)
        {
            this.Title = Title;

            if (this.Title.Contains("(BREAK)"))
            {
                Text = Title.Split("(BREAK)".ToCharArray()).ToList();

                List<string> toRemove = new List<string>();
                for (int i = 0; i < Text.Count; i++)
                {
                    if (string.IsNullOrEmpty(Text[i]))
                    {
                        toRemove.Add(Text[i]);
                    }
                }
                for (int i = 0; i < toRemove.Count; i++)
                {
                    Text.Remove(toRemove[i]);
                }
            }
            else
            {
                Text = new List<string>();
                Text.Add(Title);
            }
        }

        private bool accepted;
        public bool Accepted
        {
            get { return accepted; }
        }

        private bool denied;
        public bool Denied
        {
            get { return denied; }
        }

        public override void Update()
        {
            Engine.Input.Update();
            if (Engine.Input.IsActivePlayerButtonDown(Buttons.A) ||
                Engine.Input.IsKeyboardPress(Keys.Enter))
            {
                accepted = true;
                if (AcceptedEvent != null)
                    AcceptedEvent(this, EventArgs.Empty);
                if (Finished != null)
                    Finished(this, EventArgs.Empty);
                if (DisableAfterFinish)
                    this.Disable();
            }
            if (ShowDenyButton)
            {
                if (Engine.Input.IsActivePlayerButtonDown(Buttons.B) ||
                    Engine.Input.IsKeyboardPress(Keys.Escape))
                {
                    denied = true;
                    if (DeniedEvent != null)
                        AcceptedEvent(this, EventArgs.Empty);
                    if (Finished != null)
                        Finished(this, EventArgs.Empty);
                    if (DisableAfterFinish)
                        this.Disable();
                }
            }
        }
        public override void Draw()
        {
            for (int i = 0; i < Text.Count; i++)
            {
                Vector2 finalPos = new Vector2(Engine.RenderManager.CenterTextX(Text[i], "Kootenay", 1), (i * 50) + 100);
                Engine.RenderManager.DrawString(Text[i], 0, 255, finalPos, "Kootenay", Color.White, null);
            }
            Engine.RenderManager.DrawStringWithButton(LanguageManager.GetEngineString(2), 2, 255, new Vector2(900, 600),
                "Kootenay", Color.White, null);
            if (ShowDenyButton)
            {
                Engine.RenderManager.DrawStringWithButton(LanguageManager.GetEngineString(3), 2, 255, new Vector2(50, 600),
                    "Kootenay", Color.White, null);
            }
        }
    }
}
